// @ts-ignore
import CryptoJS from 'crypto-js'

class Crypoto {
	public EN_CH = 'en-ch'
	private KEY = CryptoJS.enc.Utf8.parse('wX$9XdJv3l2hJlhH')

	/** CBC加密 */
	encryptCBC(str: string, timestamp: any) {
		// console.log(word, '<=CBC加密')
		if (!str) {
			return str
		}
		const dataHex = CryptoJS.enc.Utf8.parse(str)

		const encrypted = CryptoJS.AES.encrypt(dataHex, this.KEY, {
			// 盐值取请求时间戳，不足16位补0
			iv: CryptoJS.enc.Utf8.parse(timestamp + '000'),
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		})
		// console.log(encrypted.toString(), '<=加密结果', srcs)

		return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
	}

	/** CBC解密 */
	decryptCBC(encryptedVal: string, timestamp: any) {
		if (!encryptedVal) {
			return encryptedVal
		}
		const decrypt = CryptoJS.AES.decrypt(encryptedVal, this.KEY, {
			iv: CryptoJS.enc.Utf8.parse(timestamp + '000'),
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		})
		return decrypt.toString(CryptoJS.enc.Utf8)
	}
}

export default new Crypoto()
